package com.szqhyxj.youxiangju.biz.card.deposit;

import com.szqhyxj.youxiangju.biz.card.CardModel;
import com.szqhyxj.youxiangju.sys.AbBasePresenter;
import com.szqhyxj.youxiangju.sys.BaseResult;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * XiaoPanYu
 * on 2018/10/31
 */
public class DepositDetailsPresenter extends AbBasePresenter implements DepositDetailsContarct.Presenter{

    private DepositDetailsContarct.View view;
    private List<Disposable> disposables = new ArrayList<>();

    public DepositDetailsPresenter(DepositDetailsContarct.View view){
        this.view = view;
    }

    @Override
    public void init(String cardNo){

        disposables.add(CardModel.getDepositCard(cardNo)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<CardModel.Card>>(){
                    @Override
                    public void accept(BaseResult<CardModel.Card> cardBaseResult) throws Exception{
                        view.dismissLoading();
                        if(cardBaseResult.businessLogicSuccess()){
                            view.showDepositDetails(cardBaseResult.getData());
                        }else {
                            view.showToast(cardBaseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.showToast(throwable.getMessage());
                        view.dismissLoading();
                    }
                }));

    }


    @Override
    public void onDestroy(){
        for (Disposable disposable : disposables) {
            if (disposable != null) {
                disposable.dispose();
            }
        }
    }
}
